package com.itheima.demo4synchronize_code;

public class count
{
    String CardId;
    int count;
    public count(String CardId,int count)
    {
        this.CardId = CardId;
        this.count = count;
    }
    public count(){};

    public String getCardId() {
        return CardId;
    }

    public void setCardId(String cardId) {
        CardId = cardId;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "count{" +
                "CardId='" + CardId + '\'' +
                ", count=" + count +
                '}';
    }

    public void drawmoney(double money) {
        //谁来取钱
        String name = Thread.currentThread().getName();
        //判断钱是否足够
        //锁对象 必须是唯一对象
        //用this 锁对象
        //静态方法用类名.class锁住对象
        synchronized (this) {
            if (this.count >= money) {
                System.out.println(name + "取钱成功，取钱金额：" + money);
                this.count -= money;
                System.out.println(name + "取钱成功，余额为：" + this.count);
            }
            else {
                System.out.println(name + "取钱失败，余额不足");
            }
        }
    }
}
